﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class WeaponObject : MonoBehaviour
{
    public Weapon weapon
    {
        set
        {
            m_Weapon = value;
        }
        get
        {
            return m_Weapon;
        }
    }
    public Ship ship { set { m_Ship = value; } get { return m_Ship; } }
    public Ship m_Ship;
    public WeaponSlotObject m_WeaponSlotObj;
    protected Weapon m_Weapon;

    Vector3 m_ArmTargetPosition;
    Vector3 m_CurrentWeaponDirection;
    float m_Angle = 0;
    float m_TurnRange = 270;

    protected virtual void Update()
    {
        weapon.shotingOffset -= 10 * Time.deltaTime;
        weapon.shotingOffset = Math.Max(0, weapon.shotingOffset);

        //  同步角度
        var forward = m_WeaponSlotObj.transform.rotation * Vector3.up;
        var resultRotation = Quaternion.AngleAxis(m_Angle, Vector3.forward);
        m_CurrentWeaponDirection = resultRotation * forward;
        if (gameObject)
            gameObject.transform.localRotation = resultRotation;

        if (m_ArmTargetPosition == new Vector3(0, 0, 0))
            return;
        //  为武器瞄准到鼠标方向
        var targetDifference = m_ArmTargetPosition - transform.position;
        var targetDirection = targetDifference.normalized;

        var m_ArmAngleDefferent = Vector3.Angle(m_CurrentWeaponDirection, targetDirection);
        if (m_ArmAngleDefferent < 1)
            return;

        //  如果目标方向和武器方向不在同一边，则巴拉巴拉
        float lr = Vector3.Cross(m_CurrentWeaponDirection, targetDirection).z;
        float targetSide = Vector3.Cross(forward, targetDirection).z;
        float weaponSide = Vector3.Cross(forward, m_CurrentWeaponDirection).z;

        if (weaponSide * targetSide < 0 &&
            m_TurnRange >= 180 &&
            m_TurnRange < 360 &&
            Vector3.Angle(forward, targetDirection) + Vector3.Angle(forward, m_CurrentWeaponDirection) > 180)
        {
            m_ArmAngleDefferent = 360 - m_ArmAngleDefferent;
            lr = -lr;
        }

        float offsetAngle = weapon.turnSpeed * Time.deltaTime;
        if (offsetAngle > Mathf.Abs(m_ArmAngleDefferent))
            offsetAngle = Mathf.Abs(m_ArmAngleDefferent);
        if (lr >= 0)
            m_Angle += offsetAngle;
        else
            m_Angle -= offsetAngle;

        m_Angle = Mathf.Clamp(m_Angle, -m_TurnRange / 2, m_TurnRange / 2);
    }
    public void Init(WeaponSlotObject weaponSlotObject)
    {
        m_WeaponSlotObj = weaponSlotObject;
        if (m_Weapon == null)
        {
            MessageShow.Instance.Error("武器实例化失败，没有武器。");
            return; 
        }
        if(GetComponent<SpriteRenderer>() == null)
        {
            gameObject.AddComponent<SpriteRenderer>();
        }
        var sprite = ResourcesManager.Instance.GetWeaponSprite(m_Weapon.typeID);
        if(sprite)
        {
            GetComponent<SpriteRenderer>().sprite = sprite;
            GetComponent<SpriteRenderer>().sortingOrder = GM.weaponRenderSort;
        }

    }

    public virtual void Fire()
    {
    }
    public void Arm(Vector3 targetPosition)
    {
        m_ArmTargetPosition = targetPosition;
    }
}